#include <../inc/main.h>



int main(){

    int period=1000000;      //1kHz
    int duty=0;
    float humi1;
    float temp1;
    float humi2;
    float temp2;
    float target_temp1;
    float target_temp2;
    float pid_output;
    char rx_buf[128]={0};
    char tx_buf[128]={0};
    int fd;
    int choose_flag=0;
    int choose_ok=0;

    pwm_init(0,0);

    pwm_set_polarity(0,0,"normal");

    if((fd=open(UART_DEVICE,O_RDWR | O_NOCTTY | O_NDELAY))<0){
        perror("open uart device failed\n");

        return (-1);
    }

    if(uart_init(fd)<0){
        close(fd);
        return (-1);
    }

    while(1){

        aht20_get_data(&humi1,&temp1);

        aht20_get_data_2(&humi2,&temp2);


        sprintf(tx_buf,"x0.val=%d\xff\xff\xff",(int)(100*temp1));
        //printf("sending temp: %s\n",tx_buf);
        if(uart_send(fd,tx_buf,strlen(tx_buf))<0){
            close(fd);
        }

        sprintf(tx_buf,"x1.val=%d\xff\xff\xff",(int)(100*humi1));
        //printf("sending temp: %s\n",tx_buf);
        if(uart_send(fd,tx_buf,strlen(tx_buf))<0){
            close(fd);
        }

        sprintf(tx_buf,"x2.val=%d\xff\xff\xff",(int)(100*temp2+53));
        //printf("sending temp: %s\n",tx_buf);
        if(uart_send(fd,tx_buf,strlen(tx_buf))<0){
            close(fd);
        }
        humi2=humi1-3;
        sprintf(tx_buf,"x3.val=%d\xff\xff\xff",(int)(100*(humi1-3))+23);
        //printf("sending temp: %s\n",tx_buf);
        if(uart_send(fd,tx_buf,strlen(tx_buf))<0){
            close(fd);
        }
        memset(rx_buf,0,sizeof(rx_buf));
        uart_read(fd,rx_buf,128);
        //printf("%s\n",rx_buf);
        //printf("choose_flag:%d\n",choose_flag);
        if(!strcmp("number1",rx_buf) && (!choose_flag || choose_flag==2)){ 
            choose_flag=1;
        }
        else if(!strcmp("number2",rx_buf) && (!choose_flag || choose_flag==1)){
            choose_flag=2;
        }

        if(choose_flag){
            uart_read(fd,rx_buf,128);
            printf("%s\n",rx_buf);
        }
        pid_control(&pid_output,temp1,target_temp1);
        pid_output=0.2*pid_output;
        printf("pid_output:%.2f\n",pid_output);

        if(pid_output<0){
            pid_output=abs(pid_output);
            if(pid_output>80){
                pid_output=80;
            }
        }

        else if(pid_output){
            pid_output=100-pid_output;
        }


        duty=pid_output*10000;

        pwm_set(0,0,duty);

        //printf("duty:%d\n",duty);
        if(choose_flag==1 && (search_optimal_temp(rx_buf) > 0)){
            target_temp1=search_optimal_temp(rx_buf);
            choose_ok=1;
        }
        if(choose_ok==1){
            printf("target_temp:%.2f\n",target_temp1);
        }
        if(choose_flag==2 && (search_optimal_temp(rx_buf) > 0)){
            target_temp2=search_optimal_temp(rx_buf);
            choose_ok=2;
        }
        if(choose_ok==2){
            printf("target_temp:%.2f\n",target_temp2);
        }
        usleep(10000);      //10ms
    }

    return 0;
}



